# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from ustv.models import TTvEpisodePlot, TVideoList, TUrlList, TSpiderVideoIndex

class TVideoListAdmin(admin.ModelAdmin):
    list_display = ('title', 'series_name', 'series_no', 'genre', 'area', 'stars', 'director', 'plots', 'updatetip', 'dsite', 'episode_count', 'intro_cnt', 'addtime', 'update_time')
    fields = ('pic', 'pic_v', 'title', 'series_name', 'series_no',  'genre', 'area', 'stars', 'director', 'introduction', 'updatetip', 'episode_count', 'lekuid', 'addtime', 'update_time')
    readonly_fields = fields
    search_fields = ('series_name', 'stars', 'director', 'dsite')
    list_filter = ('area', 'videotype')
    list_per_page = 100
    actions_on_top = True
    actions_on_bottom = False
    actions_selection_counter = False

    def pic(self, obj):
        if obj.pic_v:
            return "<img src='%s'>"%(obj.pic_v)
    pic.short_description = u'海报'
    pic.allow_tags = True

    def intro_cnt(self, obj):
        if obj.introduction is None or len(obj.introduction) < 10:
            return u"暂无简介"
        else:
            return u"共%s字，点击查看详情" % (len(obj.introduction))
    intro_cnt.short_description = u'简介统计'

    def plots(self, obj):
        plot_objs = TTvEpisodePlot.objects.filter(lekuid=obj.lekuid)
        if len(plot_objs) < obj.episode_count:
            return u"部分 %s/%s" %(len(plot_objs), obj.episode_count)
        else:
            return u"完整"
    plots.short_description = u'分集剧情'

class TUrlListAdmin(admin.ModelAdmin):
    list_display = ('title', 'seg', 'url', 'pic', 'site', 'lekuid', 'vip', 'createtime')
    fields = ('title', 'seg', 'url', 'pic', 'site', 'lekuid', 'vip', 'createtime')
    search_fields = ('title', 'site')
    readonly_fields = ('title', 'seg', 'url', 'pic', 'site', 'lekuid', 'vip', 'createtime')
    list_per_page = 100
    actions_on_top = True
    actions_on_bottom = False
    actions_selection_counter = False


class TTvEpisodePlotAdmin(admin.ModelAdmin):
    list_display = ('title', 'belong_to', 'lekuid', 'season', 'episode', 'plot')
    fields = ('title', 'lekuid', 'season', 'episode', 'plot')
    search_fields = ('title', )
    readonly_fields = ('title', )
    list_filter = ('season', 'episode', 'lekuid')
    list_per_page = 100
    actions_on_top = True
    actions_on_bottom = False
    actions_selection_counter = False
    def belong_to(self, obj):
        video_objs = TVideoList.objects.filter(lekuid=obj.lekuid)
        if len(video_objs) < 1:
            return _(u'未知')
        else:
            return video_objs[0].title

class TSpiderVideoIndexAdmin(admin.ModelAdmin):
    list_display = ('name', 'origin_name', 'area', 'category', 'series', 'start_time', 'finished')
    fields = ('pic_v', 'pic_h', 'name', 'origin_name', 'alias', 'area', 'category', 'series', 'start_time', 'finished')
    search_fields = ('name','origin_name', 'alias')
    readonly_fields = ('pic_v', 'pic_h', 'name', 'origin_name', 'alias', 'area', 'category', 'start_time', 'finished')
    list_filter = ('category',)
    list_per_page = 100
    actions_on_top = True
    actions_on_bottom = False
    actions_selection_counter = False

# Register your models here.
admin.site.register(TTvEpisodePlot, TTvEpisodePlotAdmin)
admin.site.register(TVideoList, TVideoListAdmin)
admin.site.register(TUrlList, TUrlListAdmin)
admin.site.register(TSpiderVideoIndex, TSpiderVideoIndexAdmin)
